<?php

/**
 * uniBoard User Activation Page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_activate{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our User API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load Language File
		$appCore -> loadLanguageFile( 'users');
				
		// User Logged In?
		if ( $appCore -> user['id'] > 0)
		{
			// Set Event
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_users'], $appCore -> lang['user_activation_title'], 0);
			
			// Run Events
			$appCore -> output -> drawEvents();
		}
		else if ( $appCore -> banned_ip)
		{
			// Set Event
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_ip'], $appCore -> lang['user_activation_title'], 0);
			
			// Run Events
			$appCore -> output -> drawEvents( true);
		}
		else
		{	
			// Block login
			$appCore -> output -> show_login = false;
			
			// Start API
			$this -> api = new api_user( $appCore, $appCore -> _REQ('user'));
			
			// User not found?
			if ( !$this -> api -> data_loaded)
			{
				// Set Event
				$appCore -> output -> setEvent( $appCore -> lang['api_user_not_found'], $appCore -> lang['user_activation_title'], 0);
				
				// Run Events
				$appCore -> output -> drawEvents();
			}
			else if ( $this -> api -> user_activation == 0)
			{
				// Set Event
				$appCore -> output -> setEvent( $appCore -> lang['user_activate_0'], $appCore -> lang['user_activation_title'], 1);
				
				// Run Events
				$appCore -> output -> drawEvents( true, false);
			}
			else if ( $this -> api -> user_activation == 2)
			{
				// Set Event
				$appCore -> output -> setEvent( $appCore -> lang['user_activate_2'], $appCore -> lang['user_activation_title'], 0);
				
				// Run Events
				$appCore -> output -> drawEvents( true);
			}
			if ( strlen( $appCore -> _REQ('code')) > 0) // Activate user
			{
				// Code Match?
				if ( $appCore -> _REQ('code') == $this -> api -> user_code)
				{
					// Change Group?
					if ( $this -> api -> user_activation == 1)
					{
						// Change user group to 3 - registered
						$this -> api -> setGroupId( 3);
						$this -> api -> setUserGroups(array( 3));
						
						// Nail all User Group Memberships
						$appCore -> db -> delete( 'user_groups_members', 'user_id = \'' . $this -> api -> user_id . '\'');
						
						// Make him registered member
						$appCore -> db -> insert( array('group_id' => 3, 'user_id' => $this -> api -> user_id), 'user_groups_members');
					}
					
					// Change User Activation to 0
					$this -> api -> setUserActivation( 0);
					
					// Update Api
					$this -> api -> saveData();
					
					// Set Event
					$appCore -> output -> setEvent( $appCore -> lang['user_activate_done'], $appCore -> lang['user_activation_title'], 1);
					
					// Run Events
					$appCore -> output -> drawEvents( true, false);
				}
				else
				{
					// Set Event
					$appCore -> output -> setEvent( $appCore -> lang['user_activate_code_incorrect'], $appCore -> lang['user_activation_title'], 0);
					
					// Repeat Form
					$this -> drawForm();
				}
			}
			else // Just draw activation form... for teh lulz
			{
				$this -> drawForm();
			}
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Manual Activation Form
	 *
	 */
	
	function drawForm(){
				
		// Load Templates
		$templates = new tpl_register_page( $this -> appCore);
				
		// Draw Template
		$this -> appCore -> output -> draw( $this -> appCore -> lang['user_activation_title'], array(
			array( $this -> appCore -> niceLink( 'act=activate&user=' . ((integer) $this -> appCore -> _REQ( 'user')), $this -> appCore -> lang['user_activation_title']),  $this -> appCore -> lang['user_activation_title'])
		), $templates -> tpl_activate_form( array( 'user_id' => (integer) $this -> appCore -> _REQ( 'user'), 'user_code' => $this -> appCore -> _REQ( 'code'))));
	
	}
	
}